package cn.xfdzx.android.apps.shop.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.ShopInOrderAfterSalesListAdapter;
import cn.xfdzx.android.apps.shop.adapter.ShopInOrderListAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.bean.ShopInOrderAfterSalesListBeen;
import cn.xfdzx.android.apps.shop.bean.ShopInPiFaOrderAfterSalesListBeen;
import cn.xfdzx.android.apps.shop.bean.ShopOrderListBeen;
import cn.xfdzx.android.apps.shop.contract.ShopInOrderAfterSalesListContract;
import cn.xfdzx.android.apps.shop.contract.ShopInOrderListContract;
import cn.xfdzx.android.apps.shop.evenbus.SearchGoodsContentMessage;
import cn.xfdzx.android.apps.shop.evenbus.UpdateShopOrderMessage;
import cn.xfdzx.android.apps.shop.presenter.ShopInOrderAfterSalesListPresenter;
import cn.xfdzx.android.apps.shop.presenter.ShopInOrderListPresenter;
import cn.xfdzx.android.apps.shop.util.KeybordUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.UtilsData;

/**
 * @author gzk
 * @date 2020/3/26 0026
 * description： 订单管理 零售售后
 */
public class ShopInOrderAfterSalesFragment extends BaseMvpFragment<ShopInOrderAfterSalesListPresenter> implements ShopInOrderAfterSalesListContract.View {

    RecyclerView rvAllOrder;
    SmartRefreshLayout mRefreshLayout;


    private ShopInOrderAfterSalesListAdapter orderListAdapter;
    private int page = 1;
    private int limit = Constants.LIMIT;
    private String orderType, styleType;
    private List<ShopInOrderAfterSalesListBeen.DataBean.ApplyBean> mList = new ArrayList();

    private LinearLayout llHint;
    private TextView tvDaiShenhe;
    private TextView tvShenheGuo;
    private TextView tvShenheBohui;
    private String storeId = "";

    private String status = "";
    private String keyWord = "";
    private TextView select_textView;


    @Override
    protected void initView(View view) {
        EventBus.getDefault().register(this);

        rvAllOrder = view.findViewById(R.id.rv_all_order);
        mRefreshLayout = view.findViewById(R.id.refreshLayout);
        llHint = view.findViewById(R.id.ll_hint);
        tvDaiShenhe = view.findViewById(R.id.tv_dai_shenhe);
        tvShenheGuo = view.findViewById(R.id.tv_shenhe_guo);
        tvShenheBohui = view.findViewById(R.id.tv_shenhe_bohui);

        orderType = getArguments().getString("orderType");
        styleType = getArguments().getString("styleType");
        storeId = UtilsData.getPreference(getActivity(), "login").getString("storeId");
//        storeId = "131530";
        mPresenter = new ShopInOrderAfterSalesListPresenter();
        mPresenter.attachView(this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvAllOrder.setLayoutManager(linearLayoutManager);
        rvAllOrder.setItemAnimator(new DefaultItemAnimator());
        orderListAdapter = new ShopInOrderAfterSalesListAdapter(getContext(), mList, styleType);

        rvAllOrder.setAdapter(orderListAdapter);


        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.initData(true, false, storeId, styleType, status, keyWord, page + "", limit + "");
            }
        });

        if (styleType.equals("0")) {

        } else {

        }

        tvDaiShenhe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == select_textView) {
                    select_textView = tvDaiShenhe;
                    status = "1";
                    tvDaiShenhe.setTextColor(getResources().getColor(R.color.color_FFFFFF));
                    tvDaiShenhe.setBackgroundResource(R.drawable.gradient_color_after_sales_title_bg);
                } else {
                    if (select_textView == tvDaiShenhe) {
                        select_textView = null;
                        status = "";
                        tvDaiShenhe.setBackgroundResource(R.drawable.df5f5f5_bg_r3);
                        tvDaiShenhe.setTextColor(getResources().getColor(R.color.color_333333));
                    } else {

                        select_textView.setBackgroundResource(R.drawable.df5f5f5_bg_r3);
                        select_textView.setTextColor(getResources().getColor(R.color.color_333333));
                        select_textView = tvDaiShenhe;
                        tvDaiShenhe.setTextColor(getResources().getColor(R.color.color_FFFFFF));
                        tvDaiShenhe.setBackgroundResource(R.drawable.gradient_color_after_sales_title_bg);
                        status = "1";
                    }
                }
                page = 1;
                mPresenter.initData(true, true, storeId, styleType, status, keyWord, page + "", limit + "");
            }
        });
        tvShenheGuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == select_textView) {
                    select_textView = tvShenheGuo;
                    status = "2";
                    tvShenheGuo.setTextColor(getResources().getColor(R.color.color_FFFFFF));
                    tvShenheGuo.setBackgroundResource(R.drawable.gradient_color_after_sales_title_bg);
                } else {
                    if (select_textView == tvShenheGuo) {
                        select_textView = null;
                        status = "";
                        tvShenheGuo.setBackgroundResource(R.drawable.df5f5f5_bg_r3);
                        tvShenheGuo.setTextColor(getResources().getColor(R.color.color_333333));
                    } else {

                        select_textView.setBackgroundResource(R.drawable.df5f5f5_bg_r3);
                        select_textView.setTextColor(getResources().getColor(R.color.color_333333));
                        select_textView = tvShenheGuo;
                        tvShenheGuo.setTextColor(getResources().getColor(R.color.color_FFFFFF));
                        tvShenheGuo.setBackgroundResource(R.drawable.gradient_color_after_sales_title_bg);
                        status = "2";
                    }
                }
                page = 1;
                mPresenter.initData(true, true, storeId, styleType, status, keyWord, page + "", limit + "");
            }
        });
        tvShenheBohui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == select_textView) {
                    select_textView = tvShenheBohui;
                    status = "3";
                    tvShenheBohui.setTextColor(getResources().getColor(R.color.color_FFFFFF));
                    tvShenheBohui.setBackgroundResource(R.drawable.gradient_color_after_sales_title_bg);
                } else {
                    if (select_textView == tvShenheBohui) {
                        select_textView = null;
                        status = "";
                        tvShenheBohui.setTextColor(getResources().getColor(R.color.color_333333));
                        tvShenheBohui.setBackgroundResource(R.drawable.df5f5f5_bg_r3);
                    } else {
                        select_textView.setBackgroundResource(R.drawable.df5f5f5_bg_r3);
                        select_textView.setTextColor(getResources().getColor(R.color.color_333333));
                        select_textView = tvShenheBohui;
                        tvShenheBohui.setTextColor(getResources().getColor(R.color.color_FFFFFF));
                        tvShenheBohui.setBackgroundResource(R.drawable.gradient_color_after_sales_title_bg);
                        status = "3";
                    }
                }
                page = 1;
                mPresenter.initData(true, true, storeId, styleType, status, keyWord, page + "", limit + "");
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_shop_in_order_after_sales;
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(getActivity());
        }
    }


    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }


    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onSearchEvent(SearchGoodsContentMessage messageEvent) {
        if (messageEvent.getPage()==5) {
            KeybordUtil.closeKeybord(getActivity());
            keyWord = messageEvent.getSearchMessage();
            page = 1;
            mPresenter.initData(true, true, storeId, styleType, status, keyWord, page + "", limit + "");
        }

    }

    @Override
    public void onError(Throwable throwable) {
        Log.e("解析出错", throwable.toString());
    }

    @Override
    public void onShopInOrderAfterSalesListSuccess(ShopInOrderAfterSalesListBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (page == 1) {
                mList.clear();
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        mPresenter.initData(false, false, storeId, styleType, status, keyWord, page + "", limit + "");
                    }
                });
                tvDaiShenhe.setText("待平台审核(" + bean.getData().getCount().getReviewed() + ")");
                tvShenheGuo.setText("平台审核通过(" + bean.getData().getCount().getApproved() + ")");
                tvShenheBohui.setText("平台审核驳回(" + bean.getData().getCount().getReject() + ")");
            }
            if (bean.getData().getApply().size() == 0) {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                page++;
            }
            mList.addAll(bean.getData().getApply());
            orderListAdapter.notifyDataSetChanged();
            if (mList.size() == 0) {
                llHint.setVisibility(View.VISIBLE);
            } else {
                llHint.setVisibility(View.GONE);
            }
        } else {
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onPifaShopInOrderAfterSalesListSuccess(ShopInPiFaOrderAfterSalesListBeen bean) {

    }


    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);//加载完成
    }


    @Subscribe(threadMode = ThreadMode.POSTING)
    public void XXX(UpdateShopOrderMessage messageEvent) {
        if (messageEvent.getMessage().equals(orderType)) {
            page = 1;
            mPresenter.initData(true, true, storeId, styleType, status, keyWord, page + "", limit + "");

        }
    }

    public static ShopInOrderAfterSalesFragment newInstance(String orderType, String styleType) {
        Bundle bundle = new Bundle();
        bundle.putString("orderType", orderType + "");
        bundle.putString("styleType", styleType + "");
        ShopInOrderAfterSalesFragment fragment = new ShopInOrderAfterSalesFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDestroyView() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }

        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        mList.clear();
        mList = null;
        super.onDestroy();
    }


}
